define(['app'], function(app) {
	'use strict';

	var controller = function controller($scope, Proconsultproblem, Auth, GoCatTypeAuth,GoDialog, $window, $timeout, $ionicSideMenuDelegate) {
		$scope.isLoad = false; //可以加载
		//画面加载
		$scope.$on('$ionicView.afterEnter', function() {
			//动态设置高度
			var all = $window.innerHeight;
			//计算滚动条
			$scope.myObj = {
				"height": all - 44 + "px"
			};
		}, false);
		
		//数据模型
		if($scope.input == undefined){
			$scope.input = new Object();
		}
		$scope.isLoad = false; //可以加载
		$scope.input.nPage = 1;
		$scope.input.key = "";
		$scope.input.cat_type_id = "";
		
		//下拉刷新 判断哪个模块进行刷新
		$scope.doRefresh = function() {
			//防止重复加载
			$scope.isLoad = true;
			Proconsultproblem.getlist($scope.input.key,$scope.input.cat_type_id,1,function(data) {
				if(data!=null){
					$scope.list = data;
				}else{
					$scope.list = new Object();					
				}
				$scope.input.nPage = 1;
			},
			function() {
				$scope.$broadcast('scroll.refreshComplete');
				$timeout(function() {
				$scope.isLoad = false;
			}, 1500);
			});
		};
		//加载更多
		$scope.doRefreshMore = function() {
			//防止重复加载
			$scope.isLoad = true;
			Proconsultproblem.getlist($scope.input.key,$scope.input.cat_type_id,$scope.input.nPage+1,function(data) {
				if(data!=null){
					if($scope.list!=undefined){
						$scope.list.article = $scope.list.article.concat(data.article);
						$scope.input.nPage += 1;
					}
					else{
						$scope.list = data;
					}
				}
			},
			function() {
				$scope.$broadcast('scroll.infiniteScrollComplete');
				$timeout(function() {
				$scope.isLoad = false;
			}, 1500);
			});
		};
		//侧边菜单
		$scope.rightSideMenu = function() {
			$ionicSideMenuDelegate.toggleRight();
		};
		
		//获取地区id，省/市/区:区id
		$scope.geTregionId = function(){
//			if($scope.input.cat3){
//      		 $scope.cat_type_id=$scope.input.cat1.name+"/"+$scope.input.cat2.name+"/"+$scope.input.cat3.name+":"+$scope.input.cat3.id;
//      	}else if($scope.input.cat2){
//      		 $scope.input.cat_type_id=$scope.input.cat1.name+"/"+$scope.input.cat2.name+":"+$scope.input.cat2.id;
//      	}else if($scope.input.cat1){
//      		 $scope.input.cat_type_id=$scope.input.cat1.name+":"+$scope.input.cat1.id;
//      	}
			//该接口只需传最后一级的id即可
			if($scope.input.cat3){
        		 $scope.cat_type_id=$scope.input.cat3.id;
        	}else if($scope.input.cat2){
        		 $scope.input.cat_type_id=$scope.input.cat2.id;
        	}else if($scope.input.cat1){
        		 $scope.input.cat_type_id=$scope.input.cat1.id;
        	}
		}
		
		//查询
		$scope.doSearch = function() {
			$scope.geTregionId();
			Proconsultproblem.getlist($scope.input.key,$scope.input.cat_type_id,1,function(data){
				$scope.input.nPage = 1;
				$scope.list = data;
			});
			$ionicSideMenuDelegate.toggleRight();
		};
		//取消
		$scope.doCancel = function() {
			$ionicSideMenuDelegate.toggleRight();
		};
		
		//加载数据
		$scope.init_data = function() {
		  	Proconsultproblem.getlist($scope.input.key,$scope.cat_type_id,1,function(data){
		    	$scope.list=data;
			});
		}
		$scope.init_data(); //必须在定义之后
			/***分类三级联动***/
			$scope.input.cat1 = { id: 0, name: "请选择大分类" };
			$scope.input.cat2 = { id: 0, name: "请选择中分类" };
			$scope.input.cat3 = { id: 0, name: "请选择小分类" };
			//下拉侦听
			$scope.listen_cat1 = function (data) {
				$scope.input.cat2 = { id: 0, name: "请选择中分类" };
				$scope.input.cat3 = { id: 0, name: "请选择小分类" };
				$scope.cat3_items = '';
			};
			$scope.listen_cat2 = function (data) {
				$scope.input.cat3 = { id: 0, name: "请选择小分类" };
			};
			$scope.doSelectCat1 = function () {
				//id是值 name是显示
				GoDialog.showSelect("大分类", $scope.cat1_items, $scope.input.cat1, function (data) {
					$scope.input.cat1 = data;
				});
			}
			$scope.doSelectCat2 = function () {
				//id是值 name是显示
				GoDialog.showSelect("中分类", $scope.cat2_items, $scope.input.cat2, function (data) {
					$scope.input.cat2 = data;
				});
			}
			$scope.doSelectCat3 = function () {
				//id是值 name是显示
				GoDialog.showSelect("小分类", $scope.cat3_items, $scope.input.cat3, function (data) {
					$scope.input.cat3 = data;
				});
			}
		//定义绑定
		var items_cat = new Array();
		var obj1_cat = { name: 'input.cat1' };
		var obj2_cat = { name: 'input.cat2' };
		var obj3_cat = { name: 'input.cat3' };
		items_cat.push(obj1_cat);
		items_cat.push(obj2_cat);
		items_cat.push(obj3_cat);
		GoCatTypeAuth.init_load_only($scope, items_cat);
	}

	controller.$inject = ['$scope', 'Proconsultproblem', 'Auth', 'GoCatTypeAuth','GoDialog', '$window', '$timeout', '$ionicSideMenuDelegate'];
	
	return app.registerController("proconsultproblemctrl",controller);
});